#ifndef _MY_UTIL_
#define _MY_UTIL_
#include<iostream>
#include<fstream>
#include<string>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<memory>
#include<sstream>
#include <jsoncpp/json/json.h>
#endif

namespace aod{
    class FileUtil{
        private:
        std::string _name; //文件路径名称
        public:
        FileUtil(const std::string name):_name(name){};
        bool Exists(){
            int ret=access(_name.c_str(),F_OK);
            if(ret!=0)
            return false;
            else
            return true;
        };  //判断文件是否存在
        size_t Size(){
            if(this->Exists()==false)
            return 0;
            struct  stat st;
            int ret=stat(_name.c_str(),&st);
            if(ret!=0)
            {
                std::cout<<"get file stat failed"<<std::endl;
                return 0;
            }
            else
            return st.st_size;
        };  //获取文件大小
        bool GetContent(std::string* body){
            std::ifstream ifs;
            ifs.open(_name,std::ios::binary);
            if(ifs.is_open()==false)
            {
                 std::cout<<"open file failed"<<std::endl;
                 return false;
            }
            size_t flen=this->Size();
            body->resize(flen);
            ifs.read(&(*body)[0],flen);
            if(ifs.good()==false)
            {
                std::cout<<"read file content failed"<<std::endl;
                ifs.close();
                return false;
            }
            ifs.close();
            return true;
        }; //读取文件到body中
        bool SetContent(const std::string& body){
            std::ofstream ofs;
            ofs.open(_name,std::ios::binary);
            if(ofs.is_open()==false)
            {
                std::cout<<"open file failed"<<std::endl;
                return false;
            }
            ofs.write(body.c_str(),body.size());
            if(ofs.good()==false)
            {
                std::cout<<"write file failed"<<std::endl;
                ofs.close();
                return false;
            }
            ofs.close();
            return true;
        };  //向文件写入数据
        bool CreateDirectory(){
            if(this->Exists())
            return true;
            mkdir(_name.c_str(),0777);
            return true;
        }; //针对目录时创建目录
    };
    class JsonUtil{
        public:
        static bool Serialize(Json::Value&value,std::string*body){
            Json::StreamWriterBuilder swb;
            std::unique_ptr<Json::StreamWriter>sw(swb.newStreamWriter());
            std::stringstream ss;
            int ret=sw->write(value,&ss);
            if(ret!=0){
                std::cout<<"write failed"<<std::endl;
                return false;
            }
            *body=ss.str();
            return true;
        }
        static bool UnSerialize(const std::string&body,Json::Value* value){
            Json::CharReaderBuilder crb;
            std::unique_ptr<Json::CharReader>cr(crb.newCharReader());
            bool ret=cr->parse(body.c_str(),body.c_str()+body.size(),value,nullptr);
            if(ret==false)
            {
                std::cout<<"parse failed"<<std::endl;
                return false;
            }
            return true;
        }
    };
}